package com.ylyl.langchain4j11chatfunctioncalling.service;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.service.AiServices;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
public class WeatherService {
    //替换成你自己的和风天气密钥
    private static final String API_KEY = System.getenv("api_key01");
    //调用url地址
    private static final String BASE_URL = "https://devapi.qweather.com/v7/weather/now?location=%s&key=%s";

    public JsonNode getWeatherV2(String city) throws Exception{

        //传入地址url、和apikey
        String url = String.format(BASE_URL,city, API_KEY);

        //使用默认的HttpClient实例
        var httpClient = HttpClients.createDefault();

        //创建工厂并将其设置为RestTemplate，开启微服务调用和风天气开发服务
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        //RestTemplate微服务调用
        String response = new RestTemplate(factory).getForObject(url, String.class);

        //解析JSON响应获得第三方和风天气预报信息
        JsonNode jsonNode = new ObjectMapper().readTree(response);
        //返回信息结果
        return jsonNode;
    }
}
